<template>
  <div class="container">
    <el-row type="flex" class="row-bg" justify="space-between" align="middle">
      <el-col :span="6"><h1>套餐类型管理</h1></el-col>
      <el-col :span="6" style="text-align: right;">
        <el-button type="primary" @click="dialogVisible = true; isEditing = false;">新增套餐类型</el-button>
      </el-col>
    </el-row>

    <el-card class="box-card">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="类型名称" />
        <el-table-column prop="total" label="套餐数量" />
        <el-table-column prop="contain" label="包含套餐"/>
        <el-table-column prop="status" label="状态"/>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle style="margin-right: 10px;" @click="editT(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteT(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row type="flex" align="middle" style="margin-top: 20px;">
        <el-col :span="24" style="text-align: right;">
          <el-pagination
            :current-page="listQuery.pageNum"
            :page-sizes="[10, 20, 30]"
            :page-size="listQuery.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listQuery.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="isEditing ? '编辑套餐类型' : '新增套餐类型'" :visible.sync="dialogVisible" width="30%" @close="resetForm">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="套餐数量" prop="total">
          <el-input-number v-model="form.total" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="包含套餐" prop="contain">
          <el-input v-model="form.contain"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="isEditing ? handleUpdate() : handleCreate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAll, create, update, deleteById } from '@/api/physical_examination/type'

export default {
  data() {
    return {
      searchVal: '', // 搜索框查询
      tableData: [], // 列表数据
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      dialogVisible: false, // 控制对话框显示/隐藏
      isEditing: false, // 标记当前是新增还是编辑
      form: {
        name: '',
        total: 0,
        contain: '',
        status: '0', // 默认状态
        id: null // 用于编辑时标识记录
      },
      rules: {
        name: [
          { required: true, message: '请输入类型名称', trigger: 'blur' }
        ],
        // 可以根据需要添加更多验证规则
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getAdminList()
    },
    getAdminList() {
      getAll().then(res => {
        const data = res['data']
        this.listQuery.total = data['total']
        this.tableData = data['list']
      }).catch(error => {
        console.error('获取列表失败:', error);
        this.$message.error('获取列表失败');
      });
    },
    handleCreate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          create(this.form).then(() => {
            this.dialogVisible = false;
            this.$message({
              message: '创建成功',
              type: 'success'
            });
            this.getAdminList(); // 刷新列表
          }).catch(error => {
            console.error('创建失败:', error);
            this.$message.error('创建失败');
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editT(row) {
      this.form = { ...row }; // 浅拷贝避免直接修改原始数据
      this.isEditing = true; // 标记为编辑模式
      this.dialogVisible = true;
    },
    handleUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          update(this.form.id, this.form).then(() => {
            this.dialogVisible = false;
            this.$message({
              message: '更新成功',
              type: 'success'
            });
            this.getAdminList(); // 刷新列表
          }).catch(error => {
            console.error('更新失败:', error);
            this.$message.error('更新失败');
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    deleteT(row) {
      this.$confirm('此操作将永久删除该套餐类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getAdminList(); // 刷新列表
        }).catch(error => {
          console.error('删除失败:', error);
          this.$message.error('删除失败');
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    resetForm() {
      this.form = {
        name: '',
        total: 0,
        contain: '',
        status: 'active',
        id: null
      };
      this.$refs['form'] && this.$refs['form'].resetFields();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getAdminList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getAdminList();
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    }
  }
}
</script>

<style scoped>
/* 根据需要添加样式 */
</style>
